package com.atguigu.dao.impl;

import com.atguigu.dao.MongoServiceConfigDao;
import com.atguigu.pojo.MongoServiceConfig;

import java.util.List;

public class MongoServiceConfigDaoImpl extends BaseDao implements MongoServiceConfigDao {
    @Override
    public int addMongoServiceConfig(MongoServiceConfig mongoServiceConfig) {
        System.out.println("MongoServiceConfigDaoImpl 程序在["+Thread.currentThread().getName()+"]线程中");
        String sql="insert into t_mongoServiceConfig(`id`,`serverId`,`serviceId`,`port`,`password`,`maxConns`,`masterNode`) values(?,?,?,?,?,?,?);";
            return update(sql,mongoServiceConfig.getId(),mongoServiceConfig.getServerId(),
                    mongoServiceConfig.getServiceId(),mongoServiceConfig.getPort(),
                    mongoServiceConfig.getPassword(),mongoServiceConfig.getMaxConns(),mongoServiceConfig.getMasterNode());
    }

    @Override
    public int deleteMongoServiceConfigById(String id) {
        String sql="delete from t_mongoServiceConfig where id=?";
        return update(sql,id);
    }

    @Override
    public int updateMongoServiceConfig(MongoServiceConfig mongoServiceConfig) {
        String sql="update t_mongoServiceConfig set `serverId`=?,`serviceId`=?,`port`=?,`password`=?,`maxConns`=?,`masterNode`=? where id=?";
        return update(sql,mongoServiceConfig.getServerId(),
                mongoServiceConfig.getServiceId(),mongoServiceConfig.getPort(),
                mongoServiceConfig.getPassword(),mongoServiceConfig.getMaxConns(),
                mongoServiceConfig.getMasterNode(),mongoServiceConfig.getId());
    }

    @Override
    public MongoServiceConfig queryMongoServiceConfigById(String id) {
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`maxConns`,`masterNode` from t_mongoServiceConfig where id=?";
        return queryForOne(MongoServiceConfig.class,sql,id);
    }

    @Override
    public List<MongoServiceConfig> queryMongoServiceConfigs() {
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`maxConns`,`masterNode`  from t_mongoServiceConfig";
        return queryForList(MongoServiceConfig.class,sql);
    }
    @Override
    public MongoServiceConfig queryMongoServiceConfigByServiceId(String serviceId){
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`maxConns`,`masterNode`  from t_mongoServiceConfig where serviceId =?";
        return queryForOne(MongoServiceConfig.class,sql,serviceId);
    }
    @Override
    public List<MongoServiceConfig> queryMongoServiceConfigsByPort(String port) {
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`maxConns`,`masterNode` from t_mongoServiceConfig where port =?";
        return queryForList(MongoServiceConfig.class,sql,port);
    }

    @Override
    public Integer queryForPageTotalCount() {
        String sql="select count(*) from t_mongoServiceConfig";
        Number serverCount=(Number) queryForSingleValue(sql);//这个语句似乎可以返回任何类型的查询
        //System.out.println(bookCount);
        return serverCount.intValue();
    }

    @Override
    public List<MongoServiceConfig> queryForPageItems(Integer begin, Integer pageSize) {
        String sql ="select `id`,`serverId`,`serviceId`,`port`,`password`,`maxConns`,`masterNode` from t_mongoServiceConfig limit ?,?";
        return queryForList(MongoServiceConfig.class,sql,begin,pageSize);

    }
}
